﻿using NB.MGJ.Service.Origins;
using NB.MGJ.Common.MVC;
using NB.MGJ.Model;
using NB.MGJ.Model.OriginsEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using NB.MGJ.Service.Product;
using NB.MGJ.Service;
using NB.MGJ.Service.AntiFake;
using System.Data;
using System.Collections;

namespace NB.MGJ.Web.Controllers
{
    public class MaterialPreviewController : Controller
    {

        private OriginsMaterialService _OriginsMaterialService;
        private OriginsModuleSEService _OriginsModuleSEService;
        private OriginsModuleService _OriginsModuleService;
        private OriginsNavService _OriginsNavService;
        private OriginsNodeService _OriginsNodeService;
        private ProductBatchService _Product_BatchService;
        public ProductService _productService;

        private FleeGoodsService _FleeGoodsService;

        private AntiFake_PageCreateService _AntiFake_PageCreateService;

        private AntiFakeVerifyService _AntiFakeVerifyService;

        private OriginsAuthModuleService _OriginsAuthModuleService;

        public MaterialPreviewController()
        {
            _OriginsMaterialService = new OriginsMaterialService();
            _OriginsModuleSEService = new OriginsModuleSEService();
            _OriginsModuleService = new OriginsModuleService();
            _OriginsNavService = new OriginsNavService();
            _OriginsNodeService = new OriginsNodeService();
            _Product_BatchService = new ProductBatchService();
            _OriginsAuthModuleService = new OriginsAuthModuleService();
            _productService = new ProductService();
            _AntiFake_PageCreateService=new AntiFake_PageCreateService();
            _AntiFakeVerifyService=new AntiFakeVerifyService();
            _FleeGoodsService=new FleeGoodsService();
        }




        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 扫码查询时会调用此接口
        /// </summary>
        /// <param name="productBatchId">产品批次ID</param>
        /// <param name="ProductId">产品ID</param>
        /// <returns></returns>
        public ActionResult ViewCenterSearch(int productBatchId, int ProductId, string Fw_CodeSecret)
        {
            var errorMsg = "";
            var batchModel = _Product_BatchService.GetModelById(productBatchId);

            batchModel.ProductId = ProductId;

            if (batchModel.MaterialId == null)
            {
                errorMsg = "请检查批次是否绑定素材！";
                return RedirectToAction("OriginsErrorPage", new
                {
                    errorMsg = errorMsg
                });
            }
            var materialModel = _OriginsMaterialService.GetModel(batchModel.MaterialId);
        
            if (materialModel == null)
            {
                errorMsg = "素材不存在，请确定该素材是否删除";
                return RedirectToAction("OriginsErrorPage", new
                {
                    errorMsg = errorMsg
                });
            }
            materialModel.ProductId = ProductId.ToString();
            materialModel.FwSecretCode = Fw_CodeSecret;

            switch (materialModel.MaterialType)
            {
                case 0:
                    return RedirectToAction("StandardEditionView", materialModel);
                case 1:
                    return RedirectToAction("GoldEdtionView", materialModel);
                case 2:
                    return RedirectToAction("PlatinumEdtionView", materialModel);
                case 3:
                    return RedirectToAction("DiamondEditionView", materialModel);
                default:
                    break;
            }
            return null;
        }






        /// <summary>
        /// 素材预览调度中心
        /// </summary>
        /// <param name="materialId">素材ID</param>
        /// <returns></returns>
        public ActionResult ViewCenter(int? materialId,int batchId=0)
        {
            var errorMsg = "";



            if (materialId == null)
            {
                errorMsg = "请检查批次是否绑定素材！";
                return RedirectToAction("OriginsErrorPage", new
                {
                    errorMsg=errorMsg
                });
            }
            var model = _OriginsMaterialService.GetModel(materialId);

            var batchModel = _Product_BatchService.GetModelById(batchId);
            if (batchModel != null)
            {
                if (batchModel.ProductBindType == 0)
                {
                    model.ProductId = batchModel.ProductId.ToString();
                }
                else
                {
                    model.ProductId ="0";
                }
                
            }
            if (model == null)
            {
                errorMsg = "素材不存在，请确定该素材是否删除";
                return RedirectToAction("OriginsErrorPage", new
                {
                    errorMsg=errorMsg
                });
            }
            switch (model.MaterialType)
            {
                case 0:
                    return RedirectToAction("StandardEditionView", model);
                case 1:
                    return RedirectToAction("GoldEdtionView", model);
                case 2:
                    return RedirectToAction("PlatinumEdtionView", model);
                case 3:
                    return RedirectToAction("DiamondEditionView", model);
                default:
                    break;
            }
            return null;
        }

        public ActionResult OriginsErrorPage(string errorMsg)
        {
            ViewBag.errorMsg = errorMsg;
            return View();
        }



        public ActionResult StandardEditionView(Origins_Material model)
        {
            var mModel = model;
            var moduleSEModel = _OriginsModuleSEService.GetModel(mModel.ModuleId);
            ViewBag.moduleSEModel = moduleSEModel;
            ViewBag.materialModel = mModel;

            Dictionary<string, string> exDic = new Dictionary<string, string>();
            if (moduleSEModel.ExtendInfo != null)
            {
                exDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(moduleSEModel.ExtendInfo);
            }

            ViewBag.Fw_SecretCode = model.FwSecretCode;

            ViewBag.exDic = exDic;
            return View();
        }

        public ActionResult GoldEdtionView(Origins_Material model)
        {
            var mModel = model;
            var moduleSEModel = _OriginsModuleSEService.GetModel(mModel.ModuleId);
            ViewBag.moduleSEModel = moduleSEModel;


            Dictionary<string, string> exdic = new Dictionary<string, string>();
            var flowList = new List<FlowSEModel>();
            if (!string.IsNullOrEmpty(moduleSEModel.ExtendInfo))
            {
                exdic = JsonConvert.DeserializeObject<Dictionary<string, string>>(moduleSEModel.ExtendInfo);
            }
            if (!string.IsNullOrEmpty(moduleSEModel.Flow))
            {
                flowList = JsonConvert.DeserializeObject<List<FlowSEModel>>(moduleSEModel.Flow);
            }


            ViewBag.exDic = exdic;
            ViewBag.flowList = flowList;

            ViewBag.Fw_SecretCode = model.FwSecretCode;

            return View();
        }

        public ActionResult PlatinumEdtionView(Origins_Material model)
        {

            return View();
        }

        public ActionResult DiamondEditionView(Origins_Material model)
        {
            ViewBag.materialModel = model;
            ViewBag.Fw_SecretCode = model.FwSecretCode;

            string Wl_Code = string.Empty;
            var dt = new DataTable();

            if (model.FwSecretCode != null)
            {
                string type = _AntiFake_PageCreateService.CheckCodeType(model.FwSecretCode);
                dt = _AntiFakeVerifyService.GetCodeModel(model.FwSecretCode, type);
                if (dt.Rows.Count != 0)
                {
                    Wl_Code = Convert.ToString(dt.Rows[0]["Wl_Code"]);
                }

            }
            
           

           ViewBag.Wl_Code=Wl_Code;
            return View();
        }


        public ActionResult ModuleIndex(int moduleId)
        {
            var moduleModel = _OriginsModuleService.GetModel(moduleId);
            ViewBag.moduleModel = moduleModel;
            return View();
        }


        public ActionResult FlowNodeListPreview (int batchId = 0, int productId = 0, string Wl_Code="")
        {
          var productModel=new Product_Product();
          if (productId != 0)
          {

              productModel = _productService.GetProModel(productId);
          }
          var productBatchModel=new Product_Batch();
          if (batchId != 0)
          {
             productBatchModel=_Product_BatchService.GetModelById(batchId);
          }
  

             
            var errorMsg = "";
            if (batchId == 0)
            {
                    errorMsg = "请检查素材是否绑定批次！";
                    return RedirectToAction("OriginsErrorPage", "MaterialPreview", new
                    {
                        errorMsg = errorMsg
                    });
            }
            else
            {
                var list = _OriginsNodeService.GetNodeAllByFlowId(batchId);
                var materialId = _Product_BatchService.GetModelById(batchId).MaterialId;
                ViewBag.materialId = materialId;
                ViewBag.batchId = batchId;

                ViewBag.Wl_Code=Wl_Code;
                ViewBag.productModel=productModel;
                ViewBag.productBatchModel=productBatchModel;
                return View(list);
            }


        }





        public ActionResult AuthPreview(int? authId)
        {

            var errorMsg = "";
            if (authId == null)
            {
                errorMsg = "请检查导航图标是否绑定认证！";
                return RedirectToAction("OriginsErrorPage", new
                {
                    errorMsg = errorMsg
                });
            }
            ViewBag.AuthId = authId;
            return View();
        }


        public ActionResult GetAuthModuleListByAuthId(int Id)
        {
            var list = _OriginsAuthModuleService.GetAuthModuleListByAuthId(Id);
            return Json(list);
        }

        public ActionResult GetNodeListByBatchId(int batchId)
        {
            var list = _OriginsNodeService.GetNodeAllByFlowId(batchId,1);
            return Json(list);
        }

        


        public ActionResult GetModuleById(int moduleId)
        {
            var model = _OriginsModuleService.GetModel(moduleId);
            return Json(model);
        }




        public ActionResult GetModuleListByMaterialId(int materialId)
        {
            var list = _OriginsModuleService.GetModuleAllByMaterialId(materialId);
            return Json(list); 
        }


        public ActionResult GetNavListByModuleId(int moduleId)
        {
            var navList = _OriginsNavService.GetNavAllByModuleId(moduleId);
            return Json(navList);
        }

        public ActionResult GetProductModelById(int productId=0)
        {
            var model = _productService.GetProModel(productId);
            if (model == null)
            {
                return Json("0");
            }
            else
            {
                return Json(model);
            }
      
        }

        public ActionResult GetProductModelAndProductBatchModel (int productId=0,int productBatchId=0)
        {
            var productModel =new Product_Product();
            var productBatchModel=new Product_Batch();
            var p = _productService.GetProModel(productId);
            var BatchModel=_Product_BatchService.GetModelById(productBatchId);

            if (p != null)
            {
            productModel=p;
            }
            if (BatchModel != null)
            {
            productBatchModel=BatchModel;
            }

            Hashtable ht = new Hashtable();

            ht.Add("productModel",productModel);
            ht.Add("productBatchModel",productBatchModel);

            return Json(ht);

        }

        public ActionResult OurLifeList (string key)
        {
            if (string.IsNullOrEmpty(key)) return Json(null);
            //  Organization_Enterprise ent = _mservice.GetMaxEnterpriseIInfo(LogOnUser.EnterpriseId);
            //if (key.Trim().Substring(0, 4) != LogOnUser.ECode.Substring(0, 4) && LogOnUser.EnterpriseId != 1)
            //{
            //    return Json(1);//该物流码不属于当前企业
            //}
            var list = _FleeGoodsService.ListGetTable(key);
            if (list.Count == 0) return Json(2);
            //上级码
            var parentCode = list[0].Wl_Code;
            string productName = _FleeGoodsService.getProductName(parentCode);
            var recordlist = _FleeGoodsService.OurLife(key, parentCode, productName);

            var query =from  item in recordlist
                        where item.Text=="入库"
                        select item;



            if (query == null) return Json(2);
            return Json(query);
        }




	}
}